package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.trade.bean.RechargeDetailOutput;
import com.sanpu.kxe.business.trade.contract.ChargerTelSetupPayContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.presenter.ChargerTelSetupPayPresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.module.paypwd.popwindow.SelectPopupWindow;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import butterknife.Bind;

/**
 * Description：充值付款信息确认页面（金额，支付方式）
 * Created by 薛勇军 on 2019/3/20.
 */

public class ChargerTelSetupPayActivity extends BaseTitleActivity implements ChargerTelSetupPayContract.View {

    @Bind(R.id.charger_amount)
    TextView vChargerAmount;
    @Bind(R.id.phone_number)
    TextView vPhoneNumber;
    @Bind(R.id.pay_type)
    TextView vPayType;
    @Bind(R.id.payment_amount)
    TextView vPaymentAmount;
    @Bind(R.id.ok)
    RoundTextView vOk;

    @Bind(R.id.telfee_layout)
    LinearLayout vTelfeeLayout;
    @Bind(R.id.charger_flow)
    TextView vChargerFlow;
    @Bind(R.id.telflow_layout)
    LinearLayout vTelflowLayout;

    private SelectPopupWindow vMenuWindow;

    private ChargerTelSetupPayContract.Presenter mPresenter;

    private String mPhoneNo, mPkgInfo;

    private int mPkgId;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_charger_setup_pay;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("订单支付");

        mPhoneNo = getIntent().getStringExtra(TradeConfig.INTENT_PHONENUMBER);
        RechargeDetailOutput.AmountPackage mAmountPackage =
                (RechargeDetailOutput.AmountPackage) getIntent().getSerializableExtra(TradeConfig.INTENT_AMOUNTPACKAGE);
        RechargeDetailOutput.PointPackage mPointPackage =
                (RechargeDetailOutput.PointPackage) getIntent().getSerializableExtra(TradeConfig.INTENT_POINTPACKAGE);
        if (mAmountPackage != null) {
            mPkgId = mAmountPackage.getPkgId();
            mPkgInfo = mAmountPackage.getPkgInfo();
            vPayType.setText("余额支付");
            vPayType.setTag("BALANCE");
            vChargerAmount.setText("" + mAmountPackage.getPkgInfo() + "元");
            vPaymentAmount.setText("" + mAmountPackage.getPkgInfo() + "元");
            vTelfeeLayout.setVisibility(View.VISIBLE);
        } else if (mPointPackage != null) {
            mPkgId = mPointPackage.getPkgId();
            mPkgInfo = mPointPackage.getPkgInfo();
            vPayType.setText("积分支付");
            vPayType.setTag("POINT");
            vChargerAmount.setText("" + mPointPackage.getPkgInfo() + "元");
            vPaymentAmount.setText("" + mPointPackage.getPkgPoint() + "积分");
            vTelfeeLayout.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(mPhoneNo)) {
            vPhoneNumber.setText(mPhoneNo);
        }

        mPresenter = new ChargerTelSetupPayPresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        UserConfigOutput configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (configOutput == null || !TextUtils.equals("1", configOutput.getPayPwd())) {
            GlobalDialog dialog = new GlobalDialog(mContext).headerIcon(R.drawable.img_warm_prompt)
                    .title("温馨提示")
                    .content("余额充值前必须设置支付密码");
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
                    if (mainProvider != null) {
                        mainProvider.openSetPayPwdOneActivity(mContext, true);
                    }
                }
            });
            dialog.show();
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        vOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vPayType.getTag() == null || !(vPayType.getTag() instanceof String)) {
                    return;
                }
                if (TextUtils.isEmpty(mPhoneNo)) {
                    ToastUtil.showTextViewPrompt("没有充值的电话号码");
                    return;
                }
                Rect rect = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                int winHeight = getWindow().getDecorView().getHeight();
                vMenuWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
            }
        });
        //余额和积分充值需要支付密码
        vMenuWindow = new SelectPopupWindow(ChargerTelSetupPayActivity.this, new SelectPopupWindow.OnPopWindowClickListener() {
            @Override
            public void onPopWindowClickListener(final String psw, boolean complete) {
                if (psw.length() < 6) return;
                String payType = (String) vPayType.getTag();
                mPresenter.onCompletePay(mPhoneNo, psw, payType, mPkgId, mPkgInfo);
                vMenuWindow.clearPsw();
            }
        });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ChargerTelSetupPayContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
